package cn.yuan.xiaoyu.mapmodule.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;

import butterknife.Bind;
import butterknife.OnClick;
import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.activity.BaseActivity;
import cn.yuan.xiaoyu.commonmodule.utils.BaiduLocationUtils;
import cn.yuan.xiaoyu.commonmodule.utils.StatusBarCompatUtils;

/**
 * Created by yukuoyuan on 2017/3/1.
 * 这是一个百度地图的界面
 */

public class BaiduMapActivity extends BaseActivity implements BaiduLocationUtils.OnLocationListener {
    @Bind(R.id.iv_toolbar_left)
    ImageView ivToolbarLeft;
    @Bind(R.id.tv_toolbar_title)
    TextView tvToolbarTitle;
    @Bind(R.id.mv_map)
    MapView mvMap;
    private BaiduMap mBaiduMap;

    @Override
    protected void bindPresenter() {

    }

    @Override
    protected void initData(Bundle extras) {
        BaiduLocationUtils baiduLocationUtils = new BaiduLocationUtils(this);
        baiduLocationUtils.setOnLocationListener(this);
        mBaiduMap = mvMap.getMap();
        //普通地图
        //  mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        //开启交通图
        mBaiduMap.setTrafficEnabled(true);
        //开启交通图
        //mBaiduMap.setBaiduHeatMapEnabled(true);

    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_baidumap;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToolbarLeft.setVisibility(View.VISIBLE);
        tvToolbarTitle.setVisibility(View.VISIBLE);
        tvToolbarTitle.setText("百度地图");
    }


    @OnClick({R.id.iv_toolbar_left, R.id.tv_toolbar_title})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toolbar_left:
                onBackPressed();
                break;
            case R.id.tv_toolbar_title:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        if (mvMap != null) {
            mvMap.onDestroy();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mvMap.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mvMap.onPause();
    }

    @Override
    public void onLocationSuccess(BDLocation location) {
        /**
         * 设置初始化进入的时候默认显示当前位置
         */
        LatLng cenpt = new LatLng(location.getLatitude(), location.getLongitude());
        //定义地图状态
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(cenpt)
                .zoom(18)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
        mBaiduMap.setMapStatus(mMapStatusUpdate);
        //定义Maker坐标点
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.main_map);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(cenpt)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);
    }
}
